﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BookiFramework.Abstracts.Security;
using BookiFramework.Files.Compression;
using BookiFramework.Files;
using BookiFramework.Security.Cryptography;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
using SaveFileDialog = Microsoft.Win32.SaveFileDialog;

namespace Booki.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly FileReader _reader = new FileReader();
        private readonly FileWritter _writter = new FileWritter();
        private readonly Zipper _zipper = new Zipper();
        private readonly ICrypter _DEScrypter = new TripleDESCrypter();
        private readonly ICrypter _AEScrypter = new AESRijndaelCrypter("12EstaClave34es56dificil489ssswf", "jesulink2537hAES");

        private string currentFile = "";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var file = new OpenFileDialog();
            bool? resultDialog = file.ShowDialog();
            if (resultDialog.HasValue && resultDialog.Value)
            {
                AppendLineToConsole("Fle Selected: {0}",file.FileName);
                currentFile = file.FileName;
                txtPanel.Text = _reader.ReadTextFromFile(currentFile);
                AppendLineToConsole("File readed");
            }
            else
            {
                AppendLineToConsole("The dialog was closed.");
            }
        }

        public void AppendLineToConsole(string line)
        {
            var sb = new StringBuilder(txtConsola.Text);
            sb.AppendLine(line);
            txtConsola.Text = sb.ToString();
        }

        public void AppendLineToConsole(string line,params string[] param)
        {
            var sb = new StringBuilder(txtConsola.Text);
            sb.AppendLine(String.Format(line,param));
            txtConsola.Text = sb.ToString();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(currentFile))
            {
                AppendLineToConsole("There isn't a open file.");
            }
            else
            {
                AppendLineToConsole(_writter.WriteText(currentFile, txtPanel.Text, true).IsCorrect
                    ? "Write successfully."
                    : "An error ocurred trying to write content.");
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            bool? resultDialog = fileDialog.ShowDialog();
            if (resultDialog.HasValue && resultDialog.Value)
            {
                AppendLineToConsole("Listing Zipping Files");
                fileDialog.FileNames.ToList().ForEach(x=> AppendLineToConsole("Fle Selected: {0}", x));

                var save = new SaveFileDialog();
                save.DefaultExt = ".zip";
                var savresult = save.ShowDialog();
                if (savresult.HasValue && savresult.Value)
                {
                    AppendLineToConsole("Fle Selected to Save: {0}", save.FileName);
                    if (_zipper.Zip(save.FileName, fileDialog.FileNames).IsCorrect)
                    {
                        AppendLineToConsole("Sucessfully zipped:{0}",save.FileName);
                    }
                    else
                    {
                        AppendLineToConsole("A error ocurred zipping file.");
                    }
                }
                else
                {
                    AppendLineToConsole("The dialog was closed.");
                }
            }
            else
            {
                AppendLineToConsole("The dialog was closed.");
            }

        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var fileOpenDialog = new OpenFileDialog();
            var result = fileOpenDialog.ShowDialog() ?? false;
            if (result)
            {
                AppendLineToConsole("Zip File Selected: {0}",fileOpenDialog.FileName);
                
                var dialog = new FolderBrowserDialog();
                var resultpick = dialog.ShowDialog();
                if (resultpick == System.Windows.Forms.DialogResult.OK)
                {
                    var folder = dialog.SelectedPath;
                    var or = _zipper.UnZipToDirectory(fileOpenDialog.FileName, folder);
                    if (or.IsCorrect)
                    {
                        AppendLineToConsole("UnZip File Successfully To: {0}", folder);
                    }
                    else
                    {
                        AppendLineToConsole("An error has ocurred.");
                    }
                }
                else
                {
                    AppendLineToConsole("The dialog was closed.");
                }

            }
            else
            {
                AppendLineToConsole("The dialog was closed.");
            }
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            var text = txtSource1.Text;
            var crypted = _DEScrypter.Crypt(text);
            AppendLineToConsole("Crypted '{0}' using TripleDES",text);
            
            var sb = new StringBuilder();
            crypted.ToList().ForEach(x=>sb.Append(x + " "));

            txtTarget1.Text = sb.ToString().Trim();
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            try
            {
                var bytes = new List<byte>();
                var result = txtSource1.Text.Split(new char[] {' '});
                result.ToList().ForEach(x=>bytes.Add(Byte.Parse(x)));

                var textresutl = _DEScrypter.Decrypt(bytes.ToArray());
                txtTarget1.Text = textresutl;
                AppendLineToConsole("Decrypted success using Triple DES");

            }
            catch (Exception ex)
            {
                AppendLineToConsole("A eror ocurred : {0}",ex.Message);
            }
        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            var text = txtSource2.Text;
            var crypted = _AEScrypter.Crypt(text);
            AppendLineToConsole("Crypted '{0}' using AES", text);

            var sb = new StringBuilder();
            crypted.ToList().ForEach(x => sb.Append(x + " "));

            txtTarget2.Text = sb.ToString().Trim();
        }

        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            try
            {
                var bytes = new List<byte>();
                var result = txtSource2.Text.Split(new char[] { ' ' });
                result.ToList().ForEach(x => bytes.Add(Byte.Parse(x)));

                var textresutl = _AEScrypter.Decrypt(bytes.ToArray());
                txtTarget2.Text = textresutl;
                AppendLineToConsole("Decrypted success using AES");

            }
            catch (Exception ex)
            {
                AppendLineToConsole("A eror ocurred : {0}", ex.Message);
            }
        }
    }
}
